﻿package  {
	
	import flash.display.MovieClip;
	import flash.events.Event;
	
	public class WAnim extends MovieClip {
		
		public var anims:Array;
		public var names:Array;
		
		
		
		public var current:int;
		
		//Events
		public var onSingleFinish:Function;
		
		
		public function WAnim() {
			super();
			// constructor code
			this.anims = new Array();
			this.names = new Array();
			this.current = 0;
			this.onSingleFinish = function(){};
			this.addEventListener(Event.ADDED_TO_STAGE, this.onStage);
		}
		
		public function onStage(e:Event):void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE, this.onStage);
		}
		
		public function clearAll():void
		{
			//TODO: remove children, new array, clearTweens
		}
		
		public function addFromLib(instance_name:String, linkto:String = ''):void
		{
			var ClassRef:Class;
			ClassRef = Helper.getClass(instance_name);
			if(ClassRef)
			{
				this.names.push(linkto);
				this.anims.push(new ClassRef());
				this.anims[this.anims.length - 1].name = 'a'+(this.anims.length - 1);
				this.anims[this.anims.length - 1].gotoAndStop(1);
				this.anims[this.anims.length - 1].visible = false;
				this.addChild(this.anims[this.anims.length - 1]);
			}
		}
		public function addClip(instance:MovieClip):void
		{
			this.anims.push(instance);
			this.anims[this.anims.length - 1].name = 'a'+(this.anims.length - 1);
			this.anims[this.anims.length - 1].gotoAndStop(1);
			this.anims[this.anims.length - 1].visible = false;
			this.addChild(this.anims[this.anims.length - 1]);
		}
		
		public function showClip(index:int):void
		{
			this.anims[index].gotoAndStop(1);
			this.anims[index].visible = true;
		}
		public function hideClip(index:int):void
		{
			this.anims[index].gotoAndStop(1);
			this.anims[index].visible = false;
		}
		public function hideAll():void
		{
			for(var i=0;i<this.anims.length;i++)
			{
				this.anims[i].gotoAndStop(1);//abuse? can disable
				this.anims[i].visible = false;
			}
		}
		
		public function startAnimByName(_name:String, repeats:int = 0, onFinish:Function = null):void
		{
			var index = this.names.indexOf(_name);
			if(index<0)
				return;
			//TODO: stops current Animations;
			this.current = index;
			this.showClip(index);
			this.onSingleFinish = onFinish;
			this.anims[index].addEventListener(Event.ENTER_FRAME, this.animEnterFrame);
			this.anims[index].play();
		}
		
		public function startAnim(index:int, onFinish:Function = null):void
		{
			this.current = index;
			this.showClip(index);
			this.onSingleFinish = onFinish;
			this.anims[index].addEventListener(Event.ENTER_FRAME, this.animEnterFrame);
			this.anims[index].gotoAndPlay(1);
		}
		public function stopAnim():void
		{
			this.anims[index].removeEventListener(Event.ENTER_FRAME, this.animEnterFrame);
			this.anims[index].gotoAndStop(1);
			this.onSingleFinish = function(){};
		}
		
		public function animEnterFrame(e:Event):void
		{
			if(this.anims[this.current].totalFrames == this.anims[this.current])
			{
				this.anims[this.current].removeEventListener(Event.ENTER_FRAME, this.animEnterFrame);
				this.anims[this.current].stop();
				//we at the end of animation;
				this.anims[this.current].visible = false;
				//this.onSingleFinish();
			}
		}
		
		
		//SEQUENCES SECTIONS
		
		public function clearSequence():void
		{
			
		}
		
		public function addTask(info:Object):void
		{
			
		}
		
		public function startSequence():void
		{
			
		}
		
		public function stopSequence():void
		{
			
		}
		
		
		
	}
	
}
